package com.ruizhong.centercontroll.service.impl;


import com.ruizhong.centercontroll.service.FileService;
//import com.ruizhong.centercontroll.util.MinioUtilS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class FileServiceImpl implements FileService {

//    @Autowired
//    private MinioUtilS minioUtilS;
//    @Value("${minio.endpoint}")
//    private String address;
//    @Value("${minio.bucketName}")
//    private String bucketName;


    /**
     * @author:  fengweijie
     * @methodsName: uploadFile
     * @description: 上传文件到服务器
     * @param:  file，serverIp
     * @return: String
     * @throws:
     */
    @Override
    public String uploadFile(MultipartFile file, String serverIp) {
//        minioUtilS.changeMinioClient("http://"+serverIp+":9000");
//        List<String> upload = minioUtilS.upload(new MultipartFile[]{file});
//        if (CollectionUtils.isEmpty(upload)){
//            return null;
//        }
//        String url = bucketName+"/"+upload.get(0);
        if (file.isEmpty()) {
            return null;
        }

        // 获取文件名并构建存储路径
        String fileName = file.getOriginalFilename();
        File dest = new File(System.getProperty("user.dir")+"/assets", fileName);

        try {
            // 将文件内容写入到目标文件
            file.transferTo(dest);
            System.out.println("文件上传成功: " + fileName);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件上传失败: " + e.getMessage());
        }
        log.info("上传成功");
        return "assets/"+fileName;
    }

    /**
     * @author:  fengweijie
     * @methodsName: deleteFile
     * @description: 根据路径删除文件
     * @param:  fileList - 文件名称列表
     * @return: String
     * @throws:
     */
    @Override
    public void deleteFile(List<String> fileList) throws Exception {
        List<String> filePaths = fileList.stream()
                .map(filePath -> filePath.substring(filePath.lastIndexOf("/") + 1))
                .collect(Collectors.toList());
//        minioUtilS.removeObjects(bucketName, filePaths);
        log.info("删除成功");
    }
}
